/*
 * Предок
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include <ctype.h>

/* {{{ doubly-linked list */
struct dll_node {
	int key;
	struct dll_node *prev, *next;
};

/*
 * This function inserts one node pointed to by 'ins' _after_ 'node'.
 * And it doesn't give a rat's ass about anything else.
 */
struct dll_node *
dll_insert(struct dll_node *node, struct dll_node *ins)
{
	assert(node != NULL);
	if (ins == NULL) {
		fprintf(stderr, "dll_insert(): ins is NULL, not doing anything\n");
		return node;
	}

	if (node->next) {
		assert(node->next->prev == node);
		node->next->prev = ins;
	}
	ins->next = node->next;
	ins->prev = node;
	node->next = ins;
	return ins;
}

/*
 * This function extracts 'node' from a doubly-linked list and returns pointer
 * to that node, which pointers '->prev' and '->next' are not reset.
 *
 * If you really want to get rid of a node, call
 * 	free(dll_delete(unneeded_node));
 */
struct dll_node *
dll_delete(struct dll_node *node)
{
	if (!node) {
		fprintf(stderr, "dll_delete(): received NULL ptr, wtf am i supposed to delete?\n");
		return NULL;
	}
	if (node->prev) {
		assert(node->prev->next == node);
		node->prev->next = node->next;
	}
	if (node->next) {
		assert(node->next->prev == node);
		node->next->prev = node->prev;
	}
	return node;
}
/* }}} */

/* {{{ adjlist w/ doubly-linked list */
struct adjlist_dll {
	struct dll_node *head;
	struct dll_node *tail;
	int _list_len;

	int visited;
	int color;
	int distance;
	int prev;
};
/* {{{ functions */
void
adjl_dll_connect(struct adjlist_dll *node, int conid)
{
	struct dll_node *wanderer;

	wanderer = (struct dll_node *) malloc(sizeof *wanderer);
	wanderer->key = conid;
	wanderer->prev = wanderer->next = NULL;

	if (node->head == NULL) {
		assert(node->tail == NULL);
		node->head = node->tail = wanderer;
		node->_list_len = 1;
	} else {
		dll_insert(node->tail, wanderer);
		node->_list_len++;
	}
}

void
adjl_dll_cleanup(struct adjlist_dll *al)
{
	struct dll_node *nx = al->head, *pr = al->head;

	while (nx) {
		pr = nx;
		nx = nx->next;
		free(pr);
	}
	al->_list_len = 0;
}
/* }}} */
/* }}} */

int
dfs_walk_graph(struct adjlist_dll *al, const int idx, int *time, int *in_t, int *out_t)
{
	struct dll_node *nd;

	in_t[idx] = (*time)++;
	al[idx].visited = 1;
	nd = al[idx].head;
	while (nd) {
		if (!al[nd->key].visited) {
			al[nd->key].prev = idx;
			dfs_walk_graph(al, nd->key, time, in_t, out_t);
		}
		nd = nd->next;
	}
	out_t[idx] = (*time)++;

	return 0;
}

int
main(void)
{
	static int in_time[100000], out_time[100000], prev[100000];
	struct adjlist_dll *al;
	int i, n, m, a, b;
	int time = 1;
	int s = 0;

	scanf("%d", &n);
	al = (struct adjlist_dll *) calloc(n, sizeof al[0]);
	for (i = 0; i < n; i++) {
		scanf("%d", &a);
		--a;
		prev[i] = a;
		if (a == -1)
			s = i;
		else
			adjl_dll_connect(al + a, i);
	}

	/*
	 * build in_time, out_time arrays
	 */
	dfs_walk_graph(al, s, &time, in_time, out_time);

	scanf("%d", &m);
	for (i = 0; i < m; i++) {
		scanf("%d%d", &a, &b);
		--a, --b;
		if (in_time[a] <= in_time[b] && out_time[b] <= out_time[a])
			puts("1");
		else
			puts("0");
	}

	for (i = 0; i < n; i++)
		adjl_dll_cleanup(al + i);
	free(al);
	return 0;
}

/*
 * M is big too, whoops
int
main(int argc, char **argv)
{
	static int in_time[100000], out_time[100000], prev[100000];
	int i, n, m, a, b;
	register int time = 0;

	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		scanf("%d", &a);
		--a;
		prev[i] = a;
	}

	scanf("%d", &m);
	for (i = 0; i < m; i++) {
		scanf("%d%d", &a, &b);
		--a, --b;
		while (prev[b] != a && prev[b] != -1) {
			b = prev[b];
		}
		if (prev[b] == a)
			puts("1");
		else
			puts("0");
	}

	return 0;
}
*/
